
{extend name='public/window' /}

{block name="content"}
<style>
    .layui-upload-drag {
        position: relative;
        padding: 10px;
        border: 1px dashed #e2e2e2;
        background-color: #fff;
        text-align: center;
        cursor: pointer;
        color: #999;
        border-radius: 50%;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="form">
                {if isset($info.id)}
                <input type="hidden" name="id" value="{$info.id}">
                {/if}
                <input type="hidden" name="__token__" id="token" value="{:token()}"/>
                <!-- 上面的不能动 -->
                <div class="layui-form-item">
                    <label class="layui-form-label"> ：</label>
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn icon-btn layui-btn-sm" id="add-course">
                            选择 
                        </button>
                        <table class="layui-hide" id="course_table" lay-filter="course_table"></table>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">学员：</label>
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn icon-btn layui-btn-sm" id="add-student">
                            选择学员
                        </button>
                        <table class="layui-hide" id="student_table" lay-filter="student_table"></table>
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin " style="width: 100%">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0px;text-align: center;">
                            <button class="layui-btn" lay-submit="" lay-filter="add">{:__('Submit')}</button>
                            <button type="reset" class="layui-btn layui-btn-primary">{:__('Reset')}</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/html" id="operationTpl">
  <a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs" data-title="删除" lay-event="del"><i class="layui-icon "></i></a>
</script>
<script>
    layui.use(['element', 'table', 'form', 'jquery','lucky','upload','laydate'], function () {
        var element = layui.element;
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var lucky=layui.lucky;




        //表单赋值
        var info = {:json_encode($info)};
        form.val("form",info);

        form.on('submit(add)', function (data) {
            var submitData = {};
            submitData.course_data = course_data.data;
            submitData.student_data = student_data.data;
            submitData.field = data.field;
            var res = lucky.FormSubmit("{:url('add')}",submitData,true);
            console.log(res)
            return false;
        });



        /** ----------------- 相关表格选择代码(开始)----------------- **/
        var course_data = [];
        var course_table = table.render({
            id:'course_parent_select',
            elem:'#course_table',
            cols: [[
                {field: 'id', title: 'ID',width:60},
                {field: 'CourseType', width:90, title: ' 类别',templet:function (rd) { if(rd.CourseType !== undefined){ return rd.CourseType.name}}},
                {field: 'CourseYear', width:90, title: '年份',templet:function (rd) {if(rd.CourseYear !== undefined){ return rd.CourseYear.name}}},
                {field: 'class_type', width:90, title: '科目类别'},
                {field: 'name', title: ' 名称'},
                {field: 'hour', width:90, title: '课时'},
                {field: 'price', width:120, title: '价格(可编辑)',edit:'text'},
                {field : 'tool',title : '操作',width : 150,align : 'center',toolbar : '#operationTpl'}
            ]],
            data: course_data
        });

        /**
         * 监听单行工具操作
         */
        table.on('tool(course_table)', function (obj) {
            var layEvent = obj.event;
            if(layEvent==="del"){
                obj.del();
            }
        });

        $('#add-course').on('click', function (){
            top.layer.open({
                title: '选择 ',
                type: 2,
                area: ['70%','70%'],
                offset: 'auto',
                maxmin: true,
                zIndex: layer.zIndex,
                skin: 'layui-layer-molv',
                shade: 0.5,
                content: "{:url('admin/Common/getCourseSelect')}",
                btn: ['确定','关闭'],
                yes: function(index, layero){
                    var dom = $(layero).find('iframe')[0].contentWindow;
                    course_data = dom.layui.table.checkStatus('get_course_select_table_id');
                    course_table.reload({data:[{}]}); //防止脏数据,清空表格
                    course_table.reload({data:course_data.data});
                    top.layer.close(index)
                }
            });
        });
        /** ----------------- 相关表格选择代码(结束)----------------- **/

        /** -----------------学员相关表格选择代码(开始)----------------- **/
        var student_data = [];
        var student_table = table.render({
            id:'student_parent_select',
            elem:'#student_table',
            cols: [[
                {field: 'id',  title: 'ID',width:60},
                {field: 'name', title: '学员名称'},
                {field: 'telephone', title: '学员手机号'},
                {field: 'id_card', title: '身份证号'},
                {field : 'tool',title : '操作',width : 60,align : 'center',toolbar : '#operationTpl'}
            ]],
            data: student_data
        });

        /**
         * 监听单行工具操作
         */
        table.on('tool(student_table)', function (obj) {
            var layEvent = obj.event;
            if(layEvent==="del"){
                obj.del();
            }
        });

        $('#add-student').on('click', function (){
            top.layer.open({
                title: '选择学员',
                type: 2,
                area: ['70%','70%'],
                offset: 'auto',
                maxmin: true,
                zIndex: layer.zIndex,
                skin: 'layui-layer-molv',
                shade: 0.5,
                content: "{:url('admin/Common/getStudentSelect')}",
                btn: ['确定','关闭'],
                yes: function(index, layero){
                    var dom = $(layero).find('iframe')[0].contentWindow;
                    student_data = dom.layui.table.checkStatus('get_student_select_table_id');
                    student_table.reload({data:[{}]}); //防止脏数据,清空表格
                    student_table.reload({data:student_data.data});
                    top.layer.close(index)
                }
            });
        });
        /** -----------------学员相关表格选择代码(结束)----------------- **/
    })
</script>

{/block}

